package icu.twtool.ui.state

import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import com.aventrix.jnanoid.jnanoid.NanoIdUtils
import icu.twtool.entity.Exercise
import icu.twtool.entity.Practice

/**
 * @author Wen
 * @since 2022-11-05
 */
class PracticeState(val exercise: Exercise) {

    /**
     * 正确的题目数
     */
    var rightCount by mutableStateOf(0)

    /**
     * 错误的算式数
     */
    var errorCount by mutableStateOf(0)

    /**
     * 当前输入的结果
     */
    var result by mutableStateOf("")

    /**
     * 当前正在做算式索引
     */
    var current by mutableStateOf(0)

    /**
     * 当前算式是否错误
     */
    var error by mutableStateOf(false)

    /**
     * 练习结果
     */
    var practice = Practice(NanoIdUtils.randomNanoId())

    /**
     * 习题算式数
     */
    val count = exercise.size

    /**
     * 当前算式的答案
     */
    val answer: String get() = exercise.get(current).answer().toString()

    val first: String get() = exercise.get(current).first.toString()


    val operation: String get() = exercise.get(current).operation.toString()

    val second: String get() = exercise.get(current).second.toString()
}

@Composable
@Suppress("FunctionName")
fun rememberPracticeState(exercise: Exercise) = remember { PracticeState(exercise) }